PGDB="postgresql://blinggroup_rw:TlT(nwTUONSGL2WTGxEu@pgm-bp1y371492cvsb5b129190.pg.rds.aliyuncs.com:1433/blinggroup?sslmode=disable"
MIG_DIR=db/migration
ARCHERY_DIR=db/archery
TARGET_DIR=dist
APP=bgs

# Go parameters
GOCMD=GO111MODULE=on go
GOBUILD=$(GOCMD) build
GODOWNLOAD=$(GOCMD) mod download

# protoc 
PB_DIR=./grpc/pb
PB_SERVICE_DIR=$(PB_DIR)/service
PB_GEN_DIR=./grpc/gen
PB_GEN_SERVICE_DIR=$(PB_GEN_DIR)/service
PROTOC_GO_OPT=paths=source_relative
PROTOC_GO-GRPC_OPT=paths=source_relative

v:
	migrate -path db/migration -database ${PGDB} version
up:
	migrate -path db/migration -database ${PGDB} -verbose up

down:
	migrate -path db/migration -database ${PGDB} -verbose down

goto:
	migrate -path db/migration -database ${PGDB} -verbose goto 2

up1:
	migrate -path db/migration -database ${PGDB} -verbose up 1

down1:
	migrate -path db/migration -database ${PGDB} -verbose down 1

dbnext:
ifdef seq
	migrate create -ext sql -dir ${MIG_DIR} -seq $(seq)
else
	migrate create -ext sql -dir ${MIG_DIR} -seq bgs
endif

schema:
	cat db/migration/*.up.sql > db/sqlc/schema.sql

archery-cur: 
	make archery seq=000003_bgs

archery:
ifdef seq
	echo '/* generated by makefile for $(seq) */\n' > $(ARCHERY_DIR)/$(seq)-schema.sql
	echo '/* $(seq).up.sql */' >> $(ARCHERY_DIR)/$(seq)-schema.sql
	cat $(MIG_DIR)/$(seq).up.sql >> $(ARCHERY_DIR)/$(seq)-schema.sql
ifneq (,$(wildcard $(MIG_DIR)/$(seq).constraint.sql))
	echo '/* $(seq).constraint.sql */' >> $(ARCHERY_DIR)/$(seq)-schema.sql
	cat $(MIG_DIR)/$(seq).constraint.sql >> $(ARCHERY_DIR)/$(seq)-schema.sql
endif
ifneq (,$(wildcard $(MIG_DIR)/$(seq).owner.sql))
	echo '/* $(seq).owner.sql */' >> $(ARCHERY_DIR)/$(seq)-schema.sql
	# cat $(MIG_DIR)/$(seq).owner.sql >> $(ARCHERY_DIR)/$(seq)-schema.sql
endif
else
	echo '/* generated by makefile for all */\n' > $(ARCHERY_DIR)/schema.sql
	echo '/* all.up.sql */' >> $(ARCHERY_DIR)/schema.sql
	cat $(MIG_DIR)/*.up.sql >> $(ARCHERY_DIR)/schema.sql
	echo '/* all.constraint.sql */' >> $(ARCHERY_DIR)/schema.sql
	cat $(MIG_DIR)/*.constraint.sql >> $(ARCHERY_DIR)/schema.sql
	# echo '/* all.owner.sql */' >> $(ARCHERY_DIR)/schema.sql
	# cat $(MIG_DIR)/*.owner.sql >> $(ARCHERY_DIR)/schema.sql
endif

sqlc:
	sqlc generate

pb:
	$(PB_DIR)/gen.sh

pbproxy_old:
	$(PB_DIR)/gen_proxy.sh

pbproxy:
	$(PB_DIR)/gen_proxy2.sh

pbproxyln:
	$(PB_DIR)/gen_proxy_ln.sh

pb-old:
	mkdir -p $(PB_GEN_DIR)/shared/message
	mkdir -p $(PB_GEN_DIR)/util/status
	mkdir -p $(PB_GEN_DIR)/service/user
	mkdir -p $(PB_GEN_DIR)/service/user_pv_log
	mkdir -p $(PB_GEN_DIR)/service/fan
	
	protoc --proto_path=$(PB_DIR)/shared \
				 --go_out=$(PB_GEN_DIR)/shared/message --go_opt=$(PROTOC_GO_OPT) \
				 message.proto
	protoc --proto_path=$(PB_DIR)/util \
				 --go_out=$(PB_GEN_DIR)/util/status --go_opt=$(PROTOC_GO_OPT) \
				 --go-grpc_out=$(PB_GEN_DIR)/util/status --go-grpc_opt=$(PROTOC_GO-GRPC_OPT) \
				 status.proto 
	protoc --proto_path=$(PB_DIR) --proto_path=$(PB_SERVICE_DIR) \
				 --go_out=$(PB_GEN_SERVICE_DIR)/user --go_opt=$(PROTOC_GO_OPT) \
				 --go-grpc_out=$(PB_GEN_SERVICE_DIR)/user --go-grpc_opt=$(PROTOC_GO-GRPC_OPT) \
				 user.proto
	protoc --proto_path=$(PB_DIR)  --proto_path=$(PB_SERVICE_DIR) \
				 --go_out=$(PB_GEN_SERVICE_DIR)/user_pv_log --go_opt=$(PROTOC_GO_OPT) \
				 --go-grpc_out=$(PB_GEN_SERVICE_DIR)/user_pv_log --go-grpc_opt=$(PROTOC_GO-GRPC_OPT) \
				 user_pv_log.proto
	protoc --proto_path=$(PB_DIR)  --proto_path=$(PB_SERVICE_DIR) \
				 --go_out=$(PB_GEN_SERVICE_DIR)/fan --go_opt=$(PROTOC_GO_OPT) \
				 --go-grpc_out=$(PB_GEN_SERVICE_DIR)/fan --go-grpc_opt=$(PROTOC_GO-GRPC_OPT) \
				 fan.proto

use:
	for f in ./pb/service/*.proto; do echo $(f);FILENAME=${f%%.*};  echo ${FILENAME};  done;
test:
	go test -v -cover ./...

dev:
	go run cmd/server/main.go

home:
	go run cmd/server/main.go -c conf/config-home.toml

mock:
	mockgen -package mockdb -destination db/mock/store.go github.com/techschool/simplebank/db/sqlc Store

download:
	$(GODOWNLOAD)

build:
	rm -rf $(TARGET_DIR)
	mkdir -p $(TARGET_DIR)
	#cp conf/config.toml $(TARGET_DIR)/config.toml
	$(GOBUILD) -o $(TARGET_DIR)/$(APP) cmd/server/main.go

clean:
	rm -rf $(TARGET_DIR)

run:
    # 启动 bgs 应用
	nohup $(TARGET_DIR)/$(APP) -c=$(TARGET_DIR)/config.toml 2>&1 > $(TARGET_DIR)/$(APP).log &

stop:
    # 关闭 bgs 应用
	pkill -f $(TARGET_DIR)/$(APP)

#.PHONY: migrateup migratedown up2 down2 sqlc test server mock